﻿
namespace Mal.Balades.Admin.Repositories
{
    public class Repository : RepositoryBase
    {
        #region Fields
        private static Repository repository;

        private CategoryRepository categories;
        private InformationTypeRepository informationTypes;
        private PictureTypeRepository pictureTypes;

        #endregion

        #region Construction / Destruction
        private Repository()
        {
            categories = new CategoryRepository();
            informationTypes = new InformationTypeRepository();
            pictureTypes = new PictureTypeRepository();
        }

        ~Repository()
        {
            Cleanup();
        }
        #endregion

        #region enum, struct, class
        public void Cleanup()
        {
            categories.Loaded += new RepositoryLoadedEventHandler(OnRepositoryLoaded);
            pictureTypes.Loaded += new RepositoryLoadedEventHandler(OnRepositoryLoaded);
            informationTypes.Loaded += new RepositoryLoadedEventHandler(OnRepositoryLoaded);
        }
        #endregion

        #region Properties
        public static Repository Instance
        {
            get
            {
                if (repository == null)
                    repository = new Repository();

                return repository;
            }
        }

        public CategoryRepository Categories
        {
            get { return categories; }
        }

        public InformationTypeRepository InformationTypes
        {
            get { return informationTypes; }
        }

        public PictureTypeRepository PictureTypes
        {
            get { return pictureTypes; }
        }
        #endregion

        #region Methods
       

        protected override void LoadRepository()
        {
            categories.Loaded += new RepositoryLoadedEventHandler(OnRepositoryLoaded);
            pictureTypes.Loaded += new RepositoryLoadedEventHandler(OnRepositoryLoaded);
            informationTypes.Loaded += new RepositoryLoadedEventHandler(OnRepositoryLoaded);

            categories.Load();
            pictureTypes.Load();
            informationTypes.Load();
        }

        
        #endregion

        #region Events
        void OnRepositoryLoaded()
        {
            if (categories.IsLoaded && pictureTypes.IsLoaded && informationTypes.IsLoaded)
            {
                Cleanup();

                this.LoadCompleted();
            }
        }
        #endregion



    }
}
